<?php

namespace backend\models;

use common\server\BasicDataServer;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\RentingModel;

/**
 * SearchRentingModel represents the model behind the search form about `common\models\RentingModel`.
 */
class SearchRentingModel extends RentingModel
{
    public  $mode = 0;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'decoration_id', 'region_id', 'is_banner', 'is_banner_home', 'creater', 'is_adv', 'is_recommend', 'recommend_sort'], 'integer'],
            [['house_name', 'house_unit_price', 'address', 'traffic', 'mating', 'lease', 'payment', 'configure', 'banner', 'adv'], 'safe'],
            [['latitude', 'longitude'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = RentingModel::find();
        $query->orderBy(['id'=>SORT_DESC]);
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        switch ($this->mode){
            case 1:
                $query->where(['is_banner'=>1]);
                break;
            case 2:
                $query->where(['is_adv' => 1]);
                break;
            case 3:
                $query->where(['is_banner_home'=>1]);
                break;
            case 4:
                $query->where(['is_recommend'=>1]);
                break;

        }

        $query->andFilterWhere(['like', 'house_name', $this->house_name]);
        if(!empty($this->region_id)){
            $query->andWhere(['region_id'=>BasicDataServer::instance()->getChildRegion($this->region_id)]);
        }
        return $dataProvider;
    }

    public function getModes(){
        return [0=>'全部', 1=>'列表banner',3=>'首页banner' , 2=>'弹出广告','4'=>'推荐楼盘'];
    }
}
